// export const baseUrl = "http://47.102.152.4:23001"
// export const baseUrl = "http://47.102.152.4:32010/AppletAPI/"
import {
	getToken,getBaseUrl,getEnableEncrypt
} from './auth'
import aes from './aes.js'
let getHeader = () => {
	return {
		access_token: getToken() || "",
		EnableEncrypt: getEnableEncrypt(),
	}
}

let func = (params) => {
	let url = ""
	for (const propName of Object.keys(params)) {
		const value = params[propName];
		var part = encodeURIComponent(propName) + "=";
		if (value !== null && typeof value !== "undefined") {
			if (typeof value === "object") {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && typeof value[key] !== "undefined") {
						let params = propName + "[" + key + "]";
						let subPart = encodeURIComponent(params) + "=";
						url += subPart + encodeURIComponent(value[key]) + "&";
					}
				}
			} else {
				url += part + encodeURIComponent(value) + "&";
			}
		}
	}
	url = url.slice(0, -1);
	return url
}

export function Get(url, data) {
	return new Promise((resolve, reject) => {
		data = encryptionParams(data)
		uni.request({
			url: getBaseUrl() + url,
			method: "GET",
			header: getHeader(),
			data,
			complete(data) {
				exceptionHandler(data).then(res => {
					resolve(res.data)
				}).catch((res) => {
					reject(res)
				})
			},
		})
	})
}

export function Post(url, data) {
	data = encryption(data)
	return new Promise((resolve, reject) => {
		uni.request({
			url: getBaseUrl() + url,
			method: "Post",
			header: getHeader(),
			data,
			complete(data) {
				exceptionHandler(data).then(res => {
					// console.log('Postres',res)
					resolve(res.data)
				}).catch((res) => {
					// console.log('Postres',res)
					reject(res)
				})
			},
		})
	})
}

export function Put(url, data) {
	data = encryption(data)
	return new Promise((resolve, reject) => {
		uni.request({
			url: getBaseUrl() + url,
			method: "PUT",
			header: getHeader(),
			data,
			complete(data) {
				exceptionHandler(data).then(res => {
					resolve(res.data)
				}).catch((res) => {
					reject(res)
				})
			}
		})
	})
}

export function Delete(url, data) {

	data = encryption(data)
	return new Promise((resolve, reject) => {
		uni.request({
			url: getBaseUrl() + url,
			method: "DELETE",
			header: getHeader(),
			data,
			complete(data) {
				exceptionHandler(data).then(res => {
					resolve(res.data)
				}).catch((res) => {
					reject(res)
				})
			}
		})
	})
}

function encryptionParams(data) {
	if (!data) return
	if (getEnableEncrypt()) {
		return {
			EncryptParam: aes.encrypt(func(data))
		}
	} else {
		return data
	}
}

function encryption(data) {
	if (getEnableEncrypt()) {
		return {
			EncryptPayload: aes.encrypt(JSON.stringify(data))
		}
	} else {
		return data
	}
}

function exceptionHandler(res) {
	return new Promise((resolve, reject) => {
		if (res.data) {
			res.data = getEnableEncrypt() ? JSON.parse(aes.decrypt(res.data.EncryptResponse)) : res.data
		}
		let code = res.data.code || res.statusCode
		switch (code) {
			case 200:
				resolve(res)
				break;
			case 401:
				uni.reLaunch({
					url: "/pages/login/index"
				})
				reject(res)
				break;
			case 400:
				uni.showToast({
					title: res.data.msg || "参数错误",
					position: "bottom",
					icon: 'none'
				})
				reject(res)
				break;
			default:
				console.log(res, "111111111111")
				uni.showToast({
					title: res.data.msg || "请求失败",
					position: "bottom",
					icon: 'error'
				})
				reject(res)
				break;
		}
	})
}